/**
 * 链表 多个元素组成的列表
 * 元素不连续存储，用next指针链接在一起
 */


import type { Node } from './Node'

const a: Node<number> = {
  next: null,
  data: 1
}

const b: Node<number> = {
  next: null,
  data: 2
}

const c: Node<number> = {
  next: null,
  data: 3
}

const d: Node<number> = {
  next: null,
  data: 4
}

a.next = b
b.next = c
c.next = d

let p: Node<number> | null = a

// 遍历链表
while (p) {
  console.log(p.data)
  p = p.next
}

// 插入数据
const e: Node<number> = {
  next: null,
  data: 5
}

// 插入到cd之间
let temp = c.next
c.next = e
e.next = temp

p = a
// 遍历链表
while (p) {
  console.log(p.data)
  p = p.next
}

// 删除c节点
b.next = e
p = a
// 遍历链表
while (p) {
  console.log(p.data)
  p = p.next
}